package com.wanjia.estate.service.bdservice.ydzfkplxdsprieservice;


import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.wanjia.estate.pojo.entity.Ydzfkplxd;
import com.wanjia.estate.utils.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;

//Excel表格添加图片
@Slf4j
@Service
public class ExcelAddQrService {
    public String ExcelAddQr(String excelPath, Ydzfkplxd ydzfkplxd) {
        // 要保存在二维码中的数据
        String sjbh = ydzfkplxd.getSjbh();
        String bm = ydzfkplxd.getBm();
        String jkdwqc = ydzfkplxd.getJkdwqc();
        String je = ydzfkplxd.getJe();
        String data = sjbh+";"+bm+";"+jkdwqc+";"+je;
        //获取excelpath
        File file = new File(excelPath);
        String absolutePath = file.getAbsolutePath();
        String name = absolutePath.split("\\.")[0];
        String filePath = name+"qrcode.png"; // 保存二维码的文件路径和名称
        QRCodeUtil.QRCodeGenerator(data,filePath);//生成二维码
        log.info("二维码生成中...");

        //创建Workbook实例
        Workbook workbook = new Workbook();
        //加载Excel文档
        workbook.loadFromFile(excelPath);
        //获取第一张工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //添加图片到工作表的指定位置
        ExcelPicture pic1 = sheet.getPictures().add(1, 1,filePath);

        //设置图片的宽度和高度
        pic1.setWidth(100);
        pic1.setHeight(100);

        //保存文档
        workbook.saveToFile(excelPath, ExcelVersion.Version2013);

        return absolutePath;
    }
}
